"""Graphical user interface for game"""
import logging, pygame, os
log = logging.getLogger( 'XOMuncher.graphics' )
log.setLevel( logging.DEBUG )

from olpcgames import pangofont
import olpcgames
from gettext import gettext as _ 

#import gamestate 
    
def drawBoard(screen, gamestate = None, fgcolor = (0,0,0), bgcolor = (225,225,225)):
    """Draw the game board""" 
    """Notes: All int conversions [int()] are for shutting up python's DeprecationWarning
              TODO: Handle multiple players/monsters at same coord
              TODO: Get local player, to display only his/her data 
    """
    if gamestate:
        board = gamestate.b
        players = gamestate.PlayerList
        monsters = gamestate.MonsterList
        HEIGHT = b.HEIGHT
        WIDTH = b.WIDTH
        title_text = board.description
    else:
        board = ["2 + 5"] * 100 
        board[2] = None
        players = ["P1",0,0,1000,(0,0,255),1] + \
                  ["P2",1,2,2000,(255,0,0),1] + \
                  ["P3",2,2,3000,(0,255,0),1] + \
                  ["P4",1,3,4000,(255,0,255),1]
        monsters = ["M1",0,1] + \
                   ["M2",2,3] + \
                   ["M3",4,4]
        WIDTH = 8 
        HEIGHT = 5 
        title_text = "Multiples of 3"    
        this_player = players[0:6] #XXX IMPORTANT- how do I get which player is local? 
        
    #Start with the grid
    grid = pygame.Surface((int(screen.get_width() * 0.95), int(screen.get_height() * 0.70))) #Covers 95% of width & 70% of height- completely arbitrary
    grid.fill(bgcolor)
    for i in range(0, WIDTH + 1): #Vertical lines
        x = (grid.get_width() / WIDTH * i)
        pygame.draw.line(grid, fgcolor, (x,0), (x,grid.get_height()), 3)
    for i in range(0, HEIGHT + 1):  #Horizontal lines
        y = (grid.get_height() / HEIGHT * i)
        pygame.draw.line(grid, fgcolor, (0,y), (grid.get_width(),y), 3)
    pygame.draw.rect(grid, fgcolor, (0,0,grid.get_width(),grid.get_height()), 3); #To even it out a bit

    #Grid text
    max_len = 1;
    for i in board:
        if len(_(str(i))) > max_len:
            max_len = len(_(str(i))) 
    if max_len is 1:
        text_size = grid.get_width() / WIDTH / max_len / 4
    else:
        text_size = grid.get_width() / WIDTH / max_len        
    font = pangofont.PangoFont(family=None, size=text_size)
    for i in range(0, HEIGHT):
        for j in range(0, WIDTH):
            x = (2 * j * grid.get_width() + grid.get_width()) / (2 * WIDTH)
            y = (2 * i * grid.get_height() + grid.get_height()) / (2 * HEIGHT)
            if board[i*WIDTH+j]:
                out = _(str(board[i*WIDTH+j]))
            else:
                continue        
            out = font.render(out,True,fgcolor,bgcolor)
            grid.blit(out, (x-out.get_width()/2,y-out.get_height()/2))

    #Players
    for i in xrange(0,len(players),6):
        player = players[i:i+6] #XXX Temporary, if there's a better way to do this
        x = (2 * player[1] * grid.get_width() + grid.get_width()) / (2 * WIDTH)
        y = (2 * player[2] * grid.get_height() + grid.get_height()) / (2 * HEIGHT)
        radius = min(grid.get_width() / WIDTH, grid.get_height() / HEIGHT) * 0.45
        pygame.draw.circle(grid, player[4], (int(x),int(y)), int(radius), 5) #Just a circle for now

    #Monsters
    for i in xrange(0,len(monsters),3):
        monster = monsters[i:i+3] #XXX Temporary, if there's a better way to do this
        radius = min(grid.get_width() / WIDTH, grid.get_height() / HEIGHT) * 0.80
        x = (2 * monster[1] * grid.get_width() + grid.get_width()) / (2 * WIDTH) - radius / 2
        y = (2 * monster[2] * grid.get_height() + grid.get_height()) / (2 * HEIGHT) - radius / 2
        pygame.draw.rect(grid, (225,0,0), (x,y,radius,radius), 3) #Just a red square for now

    #Top (problem)
    top = pygame.Surface((screen.get_width(), int(screen.get_height() * 0.15))) #Covers top 15% of screen
    top.fill(bgcolor)
    font = pangofont.PangoFont(family=None, size=screen.get_height() * 0.05) 
    prob = font.render(_(title_text),True,fgcolor,bgcolor)
    top.blit(prob, (top.get_width()/2 - prob.get_width()/2, top.get_height()/2 - prob.get_height()/2))
    
    #Bottom (score & level)
    bottom = pygame.Surface((screen.get_width(), int(screen.get_height() * 0.15))) #Covers bottom 15% of screen
    bottom.fill(bgcolor)
    font = pangofont.PangoFont(family=None, size=screen.get_height() * 0.04)     
    score = _("Score: " + str(this_player[3]))
    score = font.render(score,True,fgcolor,bgcolor)
    bottom.blit(score, (top.get_width() * 0.025, bottom.get_height()/2 - score.get_height()/2))
    level = _("Level: " + str(this_player[5]))
    level = font.render(level,True,fgcolor,bgcolor)
    bottom.blit(level, (top.get_width() * 0.975 - level.get_width(), bottom.get_height()/2 - level.get_height()/2))
    
    #Now blit the surfaces to the screen
    screen.blit(top, (screen.get_width()/2 - top.get_width()/2, 0))
    screen.blit(grid, (screen.get_width() * 0.025, screen.get_height() * 0.15))
    screen.blit(bottom, (screen.get_width()/2 - top.get_width()/2, screen.get_height() * 0.85))    
    
def munch(screen, gamestate, player_name, ):
    
    """Munch a number- play a sound, maybe even animate something"""
    while(Wtemp > 10):
        Wtemp -= 10
        screen.fill(black)
        draw.circle(screen,white,(Xpos,Ypos),Wtemp, 5)
        display.flip()
    while(Wtemp < width):
        Wtemp += 10
        screen.fill(black)
        draw.circle(screen,white,(Xpos,Ypos),Wtemp,5)
        display.flip()
            
if __name__ == "__main__":
    logging.basicConfig()
    size = (800,600)
    screen = pygame.display.set_mode(size)
    while 1:
        screen.fill((225,225,225))
        drawBoard(screen)
        pygame.display.flip()
